clear all 

import delimited "$input\P2P_originated_loan_type.csv"
save "$output\P2P_originated_loan_type.dta", replace

********************************************************************************

clear
import delimited "$input\P2P_originated_loan_type_count.csv"
save "$output\P2P_originated_loan_type_count.dta", replace

rename v1 loantype
rename originated count

merge 1:1 loantype using "$output\P2P_originated_loan_type.dta"

rename originated amount
rename loantype purpose

replace purpose = "Short-term financing" if purpose == "short term financing"
replace purpose = "Other" if purpose == "other"
replace purpose = "External growth" if purpose == "external growth" 
replace purpose = "Investment" if purpose == "tangible investment" | purpose == "intangible investment" 
replace purpose = "Commercial growth"  if purpose == "commercial growth"

collapse (sum) amount count, by(purpose)

graph hbar amount count, over(purpose, sort(1) descending label(labsize(medium))) ///
plotr(color(white)) graphr(color(white))  ///
ylabel(,nogrid labsize(medium)) bar(1, color(emerald)) bar(2, color(gs10%40) lcolor(black)) ///
legend(region(lcolor(white) ) label(1 "Loan amount") label(2 "Number of loans") )  blabel(total,format(%9.1f))

graph save  "$figures/purpose.gph", replace
graph export  "$figures/purpose.pdf", replace
